﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace StoreWeb.ajax
{
    public partial class role : System.Web.UI.Page
    {
        Maticsoft.BLL.Role rolebll = new Maticsoft.BLL.Role();
        Maticsoft.BLL.RoleRightRelation relabll = new Maticsoft.BLL.RoleRightRelation();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                int row = 0;
                if (!string.IsNullOrEmpty(Request["action"]) && Request["action"] == "delete")
                {

                    Int32 resltCount = new Maticsoft.BLL.UserRoleRelation().GetList(" roleid=" + Request["id"] + " ").Tables[0].Rows.Count;
                    if (resltCount > 0)//如果该角色在UserRoleRelation中已使用  则无法删除
                    {
                        Response.Write("11");
                    }
                    else
                    {
                        Response.Write((rolebll.Delete(Convert.ToInt32(Request["id"])) ? "1" : "0"));
                    }

                }
                else
                {
                    Maticsoft.Model.Role md = new Maticsoft.Model.Role();
                    if (!string.IsNullOrEmpty(Request["id"]))
                    {
                        md = rolebll.GetModel(Convert.ToInt32(Request["id"]));
                        md.description = Request["note"];
                        md.rolename = Request["username"];
                        md.rolecode = Request["code"];
                        if (!string.IsNullOrEmpty(Request["extensionlist"]))
                        {
                            List<Maticsoft.Model.RoleRightRelation> alist = new List<Maticsoft.Model.RoleRightRelation>();
                            foreach (var item in Request["extensionlist"].Split('|'))
                            {
                                Maticsoft.Model.RoleRightRelation rmd = new Maticsoft.Model.RoleRightRelation();
                                rmd.rolecode = Request["code"];
                                rmd.rightscode = item.Split(',')[0];
                                rmd.extension = item.Split(',')[1];
                                alist.Add(rmd);
                            }
                            List<Maticsoft.Model.RoleRightRelation> list = relabll.GetModelList("rolecode='"+Request["code"]+"'");//获取已有的权限
                            //添加没有的权限
                            foreach (var item in alist)
                            {
                                if (!list.Any(c => c.extension == item.extension && c.rightscode == item.rightscode))//数据库该角色没有该权限
                                {
                                    relabll.Add(item);
                                }
                            }
                            //去除多余的权限
                            foreach (var item in list)
                            {
                                if (!alist.Any(c => c.extension == item.extension && c.rightscode == item.rightscode))//数据库该角色没有该权限
                                {
                                    relabll.Delete(item.id);
                                }
                            }
                        }
                        row = rolebll.Update(md) ? 2 : 0;
                    }
                    else
                    {
                        md.createtime = DateTime.Now;
                        md.description = Request["note"];
                        md.rolename = Request["username"];
                        md.rolecode = Request["code"];
                        if (!string.IsNullOrEmpty(Request["extensionlist"]))
                        {
                            List<Maticsoft.Model.RoleRightRelation> alist = new List<Maticsoft.Model.RoleRightRelation>();
                            foreach (var item in Request["extensionlist"].Split('|'))
                            {
                                Maticsoft.Model.RoleRightRelation rmd = new Maticsoft.Model.RoleRightRelation();
                                rmd.rolecode = Request["code"];
                                rmd.rightscode = item.Split(',')[0];
                                rmd.extension = item.Split(',')[1];
                                alist.Add(rmd);
                            }
                            List<Maticsoft.Model.RoleRightRelation> list = relabll.GetModelList("rolecode='" + Request["code"] + "'");//获取已有的权限
                            //添加没有的权限
                            foreach (var item in alist)
                            {
                                if (!list.Any(c => c.extension == item.extension && c.rightscode == item.rightscode))//数据库该角色没有该权限
                                {
                                    relabll.Add(item);
                                }
                            }
                            //去除多余的权限
                            foreach (var item in list)
                            {
                                if (!alist.Any(c => c.extension == item.extension && c.rightscode == item.rightscode))//数据库该角色没有该权限
                                {
                                    relabll.Delete(item.id);
                                }
                            }
                        }
                        row = rolebll.Add(md) > 0 ? 1 : 0;
                    }
                    if (row == 1)
                    {
                        Response.Write("1");
                    }
                    else if (row == 2)
                    {
                        Response.Write("2");
                    }
                    else
                    {
                        Response.Write("0");
                    }
                }
            }
        }
    }
}